package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 公共接口
 */
@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "公共接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     *
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> logout(MultipartFile file) throws IOException {
        log.info("文件上传：{}", file.getOriginalFilename());
        //1、组装文件名( 2024/1/18/7890lkjsajdfkljl134.jpg )    1.jpg
        String fileName = file.getOriginalFilename();
        fileName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date())
                  + UUID.randomUUID()
                  + fileName.substring(fileName.lastIndexOf("."));

        //2、文件上传
        String uploadPath = aliOssUtil.upload(file.getBytes(), fileName);
        return Result.success(uploadPath);
    }
}
